package ch.usi.inf.pf2.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import ch.usi.inf.pf2.gui.listener.CopyActionListener;
import ch.usi.inf.pf2.gui.listener.CutActionListener;
import ch.usi.inf.pf2.gui.listener.PasteActionListener;

/**
 * A pop up menu appearing only when the user selects a specific cell.
 * This pop up menu aims to make the application more user-friendly also
 * by helping the user to choose the formula he needs among the available
 * ones.
 * 
 * @author Ebrisa Savina Mastrodicasa
 */


public class JSpreadSheetPopupMenu extends JPopupMenu
{
	private JSpreadSheetGUI spreadTable;
	
	
	/**
	 * Create a pop up menu that appears when the user selects a cell.
	 * @param spreadTable The spread sheet on which the pop up menu is active
	 */
	public JSpreadSheetPopupMenu(JSpreadSheetGUI spreadTable)
	{
		super("Edit cell");
		this.spreadTable = spreadTable;
		makeMenu();
	}
	
	
	/**
	 * Create the pop up menu, with the edit commands
	 * and the possible formulas to be chosen.
	 */
	public void makeMenu()
	{
		// Edit popup menu: CUT 
		JMenuItem cutItemPopup = new JMenuItem("Cut");
		cutItemPopup.addActionListener(new CutActionListener(spreadTable));
		this.add(cutItemPopup);
		
		// Edit popup menu: COPY
		JMenuItem copyItemPopup = new JMenuItem("Copy");
		copyItemPopup.addActionListener(new CopyActionListener(spreadTable));
		this.add(copyItemPopup);
		
		// Edit popup menu: PASTE
		JMenuItem pasteItemPopup = new JMenuItem("Paste");
		pasteItemPopup.addActionListener(new PasteActionListener(spreadTable));
		this.add(pasteItemPopup);
		
		
		this.addSeparator();
		
		// Computing popup menu: INSERT FORMULA
		JMenuItem formulaItemPopup = new JMenuItem("Insert formula");
		formulaItemPopup.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new SpreadSheetFormulaWindow(spreadTable);			
			}
		});
		this.add(formulaItemPopup);
	}
}
